package com.onpositive.dataminer.rcp.utils;

import java.util.ArrayList;

import com.onpositive.dataminer.core.AuditManager;
import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.dataminer.core.AuditManager.ObjEntry;
import com.onpositive.dataminer.core.AuditManager.ObjSettings;
import com.onpositive.knowledge.model.value.audits.IAuditManager;
import com.onpositive.knowledge.model.values.IValueTransformer;
import com.onpositive.semantic.model.api.changes.IValueListener;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.realm.IRealm;
import com.onpositive.semantic.model.api.realm.Realm;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.binding.IBindable;
import com.onpositive.semantic.model.binding.IBinding;
import com.onpositive.semantic.model.binding.IBindingSetListener;

public class AuditManagerHelper implements IBindable {
	
	private IBinding rootBinding;
	
	private IRealm<ObjectEntry> transformerQueue;

	private String getCurrentPropertyId() {
		if(rootBinding==null)
			return null;
		
		Object value = rootBinding.getValue();
		if(!(value instanceof IProperty))
			return null;
		
		IProperty prop = (IProperty) value;
		String propId = prop.getId();
		return propId;
	}

	@Override
	public void setBinding(IBinding binding) {
		this.rootBinding = binding;	
		rootBinding.addValueListener(new IValueListener<Object>() {

			private static final long serialVersionUID = 0L;

			@Override
			public void valueChanged(Object oldValue, Object newValue) {
				Binding bnd = (Binding) getBinding().getBinding("helper.ActiveOnCurrentProperty") ;
				bnd.refresh();				
			}
		});
	}

	@Override
	public IBinding getBinding() {
		return rootBinding;
	}

	@Override
	public void addBindingSetListener(IBindingSetListener listener) {
				
	}

	@Override
	public void removeBindingSetListener(IBindingSetListener listener) {
		
	}
	
	public IRealm<ObjectEntry> getTransformerQueue(){
		
		if(transformerQueue!=null)
			return transformerQueue;
		
		DefaultUniverse universe = DefaultUniverse.getInstance();
		if(universe==null)
			return null;
		
		IAuditManager auditManager = universe.getAuditManager();
		if(!(auditManager instanceof AuditManager))
			return null;
		
		AuditManager am = (AuditManager) auditManager;
		ArrayList<ObjEntry<IValueTransformer>> tq = am.getTransformerQueue();
		Realm<ObjectEntry> r = new Realm<ObjectEntry>();
		for(ObjEntry<IValueTransformer> oe : tq){
			ObjectEntry objectEntry = new ObjectEntry(oe);
			r.add(objectEntry);
		}
		transformerQueue = r;
		return transformerQueue;
	}
	
	@SuppressWarnings("rawtypes")
	public class ObjectEntry{		
		
		AuditManager.ObjEntry entry;
		
		public ObjectEntry(ObjEntry entry) {
			super();
			this.entry = entry;
		}

		public AuditManager.ObjEntry getEntry() {
			return entry;
		}

		public void setEntry(AuditManager.ObjEntry entry) {
			this.entry = entry;
		}

		public Boolean isActiveOnCurrentProperty(){
			
			if(entry==null)
				return true;
			
			ObjSettings commonSettings = entry.getCommonSettings();
			if(commonSettings==null)
				return true;
			
			String propId = getCurrentPropertyId();
			if(propId==null)
				return true;
			
			boolean isActive = !commonSettings.isIgnored(propId);
			return isActive ;
		}
		
		public void setActiveOnCurrentProperty(Boolean val){
			
			if(val==null)
				return;
			
			ObjSettings commonSettings = entry.getCommonSettings();
			if(commonSettings==null)
				return;
			
			String propId = getCurrentPropertyId();
			if(propId==null)
				return;
			
			commonSettings.setPropertyAsIgnored(propId, !val);
		}
		
	}

}
